<?php
require_once "BaseAction.php";
class Event_AdmineventController extends TTEvent_Base_Action 
{
    
    public function init(){
        parent::init();
        if ( ! $this->isAdminEvent()){
            exit();
        }               
        $this->_helper->layout->setLayout('event_admin');
    }

    public function indexAction(){ 
       // $this->_helper->getHelper('Redirector')->gotoUrlAndExit('/event/admin/?published=0'); 
        $this->setTitle("Quản lý trang chủ sự kiện");
        $user = $this->getUser();
        $this->view->user=$user; 
        $req = $this->getRequest();
        $page = (int)$req->getQuery('page', 0);
        
        $this->view->termName = "Tìm kiếm sự kiện";        
        $adminModel = $this->loadModel('Admin');
        $category=$adminModel->getAllCategory();       
        $this->view->category=$category;     
                
        $this->view->termName = "Quản lý trang chủ sự kiện";
        $req = $this->getRequest();
        $page = (int)$req->getQuery('page', 0);      
        $search=Array();
        
        if($req->isPost()){
        $search['eid']=$req->getPost('eid');    
        $search['event_name']=$req->getPost('event_name');    
        $search['uid']=$req->getPost('uid');
        $search['published']=$req->getPost('published');    
        $search['promote']=$req->getPost('promote');
        $search['cid']=$req->getPost('cid');
        }else{
        $search['eid']=$req->getQuery('eid');    
        $search['event_name']=$req->getQuery('event_name');    
        $search['uid']=$req->getQuery('uid');
        $search['published']=(int)$this->_getParam('published');  
       // if($search['published']=="") $search['published']=0;
        $search['promote']=$req->getQuery('promote');     
        $search['cid']=$req->getQuery('cid');     
        }
        $this->view->search=$search;
        $search['eid']=(int)$search['eid'];
        $search['uid']=(int)$search['uid']; 

        $action=$this->_getParam('action1');       
        switch($action){
            case 'deleteEvent':
                {
                    $list=$this->_getParam('list');
                    if(count($list)>0){
                    $memberModel = $this->loadModel('Member');     
                    $eventModel = $this->loadModel('Event');

                        foreach($list as $eid)
                            {
                                $event_name=$adminModel->getEventName($eid);
                                $note="<a href=\"/event/detail/".$eid.">".$event_name."</a><br />";
                                $module="event"; 
                                $memberModel->removeAllEventMember($eid); 
                                $memberModel->removeAllEventRequest($eid); 
                                $eventModel->destroyEvent($eid); 
                                $adminModel->addModLog($user->uid, EVENT_WD_ADMIN_DELETE, $eid, $note,$module);
                            }
                    }else{
                        $messages[1]="Bạn cần phải chọn ít nhất 1 dòng để thực hiện tiếp";
                    }
                }
            case 'changePromote':
                {
                    $list=$this->_getParam('list');
                    $promote=$this->_getParam('select_promote');
                    $published=$this->_getParam('select_published');
                    if(count($list>0)){ 
                        if(count($list)>1 && $promote==EVENT_PROMOTE_FOCUS){
                            $messages[2]="Lưu ý :Chỉ có thể chọn một sự kiện làm Tiêu Điểm";  
                        }
                        if(count($messages[2])==0){
                            foreach($list as $eid)
                                {
                                     $adminModel->changePromoteAndPublished($eid,$promote,$published);   
                                     $event_name=$adminModel->getEventName($eid); 
                                     $note="<a href=\"/event/detail/".$eid.">".$event_name."</a><br />";
                                     $module="event"; 
                                     $adminModel->addModLog($user->uid, EVENT_WD_ADMIN_EDIT." promote", $eid, $note,$module);
                                 }
                        }
                       
                    }else{
                        $messages[1]="Bạn cần phải chọn ít nhất 1 dòng để thực hiện tiếp";
                    }   
                }
            case 'changeCategory':
                {
                    $list=$this->_getParam('list');
                    $cid=$this->_getParam('select_category');
                    if(count($list)>0){
                        foreach($list as $eid)
                            {
                                $adminModel->changeCid($eid,$cid);
                                $event_name=$adminModel->getEventName($eid); 
                                $note="<a href=\"/event/detail/".$eid.">".$event_name."</a><br />";
                                $module="event"; 
                                $adminModel->addModLog($user->uid, EVENT_WD_ADMIN_EDIT." category", $eid, $note,$module);
                            }
                    }else{
                        $messages[1]="Bạn cần phải chọn ít nhất 1 dòng để thực hiện tiếp";
                    }   
                }
        }
        $this->view->err=$messages;
        
        $this->view->htmlEventList = $this->buildEventList($search,$page);
    }
    public function deleterequestAction(){
        $this->setTitle("Xóa lời mời"); 
        $this->_helper->eventCommon->buildHtmlEventLeftMenu('cat', $user->uid, 0, $cid, false); 
        $adminModel = $this->loadModel('Admin');  
        $this->view->countrequest=$adminModel->countrequest();
        $adminModel->deleterequest();
    }
    

    public function buildEventList($search,$page=0){
        $page = (int)$page;
       // $cache_key = "html_cat_".$cid."_".$p."_".$c."_".$page;
     //   $data = $this->getCache($cache_key);

        if(!$data){
     
            $adminModel = $this->loadModel('Admin');
            $eventList = $adminModel->getListEvent($search,$page);
            $pageInfo = $adminModel->getPageInfo();
            
            $assign_list = array();
            $assign_list['event_list'] = $eventList;
            $assign_list['action'] = 'category';
            $assign_list['pager'] = $this->view->partial("pager.phtml", array("pageInfo" => $pageInfo, 'title'=>'sự kiện', "ext_param" => $this->searchCondToQuery($search), "url" => "/event/admin/".$cid));      
            $data = $this->view->partial('adminevent/event_list.phtml', $assign_list);
           // $this->setCache($cache_key, $data);
        }
        return $data;
    } 
    private function searchCondToQuery($searchCond){
        $ret = "";
        if(is_array($searchCond) && count($searchCond) >0){
            
            foreach ($searchCond as $key=>$val){
                if($val!="")
                    $ret .= ($ret!=""?"&":"")."$key=$val";
            }
        }
        return $ret;
    }   
}

